package com.bistu.xiong.factory.net;

import android.text.TextUtils;

import com.bistu.xiong.common.Common;
import com.bistu.xiong.factory.persistence.Account;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by XiongXiaoJun on 2017/12/15.
 */

public class Network {
    private static Network instance;
    private Retrofit retrofit;
    private Retrofit retrofitTuLing;
    // 全局的Gson
    private Gson gson;
    static {
        instance = new Network();
    }
    private Network(){

    }
    // 构建一个Retrofit
    public static Retrofit getRetrofit(){
        if(instance.retrofit != null){
            return instance.retrofit;
        }

        Retrofit.Builder builder = new Retrofit.Builder();
        //自定义gson解决日期格式问题
        instance.gson = new GsonBuilder()
                .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                        return new Date(json.getAsJsonPrimitive().getAsLong());
                    }
                })
                .create();

        // 得到一个OK Client
        OkHttpClient client = new OkHttpClient.Builder()
                // 给所有的请求添加一个拦截器
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        // 拿到我们的请求
                        Request original = chain.request();
                        // 重新进行build
                        Request.Builder builder = original.newBuilder();
                        if(!TextUtils.isEmpty(Account.getToken())){
                            //注入一个token
                            builder.addHeader("token",Account.getToken());
                        }
                        Request newRequest = builder.build();
                        // 返回
                        return chain.proceed(newRequest);
                    }
                })
                .build();

        instance.retrofit = builder.baseUrl(Common.Constance.API_URL)
                // 设置client
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .build();
        return instance.retrofit;
    }

    //构建一个Tuling的Retrofit
    public static Retrofit getTuLingRetrofit(){
        if(instance.retrofitTuLing != null){
            return instance.retrofitTuLing;
        }
        Retrofit.Builder builder = new Retrofit.Builder();
        instance.retrofitTuLing = builder.baseUrl(Common.Constance.TULING_API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return instance.retrofitTuLing;
    }

    /**
     * 返回一个全局的Gson，在这可以进行Gson的一些全局的初始化
     * @return Gson
     */
    public static Gson getGson() {
        return instance.gson;
    }
    /**
     * 返回一个请求代理
     * @return RemoteService
     */
    public static RemoteService remote() {
        return Network.getRetrofit().create(RemoteService.class);
    }
    /**
     * 返回一个请求代理
     * @return RemoteService
     */
    public static RemoteService remoteTuLing() {
        return Network.getTuLingRetrofit().create(RemoteService.class);
    }
}
